package com.coocaa.cirrus.common.base.util;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;

import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author bijiahao
 * @date : 2025/9/25 10:52
 * @description
 */
@Slf4j
@UtilityClass
public class BeanConvertUtil {
    public static <T> T convertMapToEntity(Map<String, Object> map, Class<T> clazz) {
        try {
            if (MapUtils.isEmpty(map)) {
                return null;
            }
            T entity = clazz.newInstance();
            Field[] fields = clazz.getDeclaredFields();
            // 遍历Map并设置对象的字段
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                String fieldName = StrUtil.toCamelCase(entry.getKey());
                Field field = Arrays.stream(fields).filter(f -> f.getName().equals(fieldName)).findFirst().orElse(null);
                // 使用反射设置字段值
                if (null != field) {
                    field.setAccessible(true);
                    // 这里可能需要额外的类型检查和转换
                    field.set(entity, entry.getValue());
                }
            }
            return entity;
        } catch (Exception e) {
            log.error("convertMapToEntity error", e);
            return null;
        }
    }

    public static HashMap<String, Object> convertEntityToMap(Object entity) {
        HashMap<String, Object> res = new HashMap<>();
        try {
            Field[] fields = entity.getClass().getDeclaredFields();
            for (Field field : fields) {
                String key = StrUtil.toUnderlineCase(field.getName());
                field.setAccessible(true);
                res.put(key, field.get(entity));
            }
            return res;
        } catch (IllegalAccessException e) {
            log.error("convertEntityToMap error", e);
            return res;
        }
    }

    public static List<String> getColumnNames(Class<?> clazz) {
        Field[] fields = clazz.getDeclaredFields();
        List<String> res = Arrays.stream(fields).map(field -> StrUtil.toUnderlineCase(field.getName())).collect(Collectors.toList());
        return res;
    }


    public static Gson getGson() {
        Gson gson = new Gson();
        return gson;
    }

    public static <T> List<JsonObject> buildData(List<T> data) {
        Gson gson = BeanConvertUtil.getGson();
        return data.stream().map(item -> {
            Map<String, Object> objectMap = convertEntityToMap(item);
            JsonObject jsonObject = gson.toJsonTree(objectMap).getAsJsonObject();
            return jsonObject;
        }).collect(Collectors.toList());
    }

    public static List<JsonObject> buildDataByObj(Object entity) {
        try {
            Gson gson = BeanConvertUtil.getGson();
            Map<String, Object> objectMap = convertEntityToMap(entity);
            JsonObject jsonObject = gson.toJsonTree(objectMap).getAsJsonObject();
            List<JsonObject> jsonObjectList = new ArrayList<>();
            jsonObjectList.add(jsonObject);
            return jsonObjectList;
        } catch (Exception e) {
            log.error("buildDataByObj : " + JSON.toJSONString(entity));
            throw new RuntimeException(e);
        }
    }
}
